package com.tang.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tang.dto.RoleListDto;
import com.tang.enums.AppHttpCodeEnum;
import com.tang.sys.ResponseResult;
import com.tang.sys.entity.Menu;
import com.tang.sys.entity.Role;
import com.tang.sys.entity.RoleMenu;
import com.tang.sys.mapper.RoleMapper;
import com.tang.sys.mapper.RoleMenuMapper;
import com.tang.sys.service.IMenuService;
import com.tang.sys.service.IRoleMenuService;
import com.tang.sys.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tang.sys.vo.AdminAddRoleVo;
import com.tang.sys.vo.MenuVo;
import com.tang.sys.vo.PageVo;
import com.tang.sys.vo.RoleVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author shankaka
 * @since 2023-04-19
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Resource
    private RoleMapper roleMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private IRoleMenuService iRoleMenuService;

    @Override
    public List<String> selectRoleKeyByUserId(Long id) {
        //判断是否是管理员 如果是返回集合中只需要有admin
        if(id == 1){
            List<String> roleKey = new ArrayList<>();
            roleKey.add("admin");
            return roleKey;
        }
        //如果不是否则查询所具有的角色信息


        return getBaseMapper().selectRoleKeyByUserId(id);
    }

    @Override
    public ResponseResult<PageVo> getAllRole(Integer pageNum, Integer pageSize, RoleListDto roleListDto) {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(roleListDto.getRoleName()),Role::getRoleName,roleListDto.getRoleName());
        wrapper.eq(StringUtils.hasText(roleListDto.getStatus()),Role::getStatus,roleListDto.getStatus());
        wrapper.orderByAsc(Role::getRoleSort);
        Page<Role> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);

        page(page,wrapper);

        PageVo pageVo = new PageVo(page.getRecords(),page.getTotal());

        return ResponseResult.okResult(pageVo);
    }
    //修改状态
    @Override
    public ResponseResult<RoleVo> updateStatus(RoleVo roleVo) {
        Role role = roleMapper.selectById(roleVo.getRoleId());
        role.setStatus(roleVo.getStatus());

        roleMapper.updateById(role);

        return ResponseResult.okResult();
    }

    @Override
    public void addRoleMenu(Role role) {
        role.setCreateTime(new Date());

        //写入角色
        this.baseMapper.insert(role);
        //写入角色菜单关系表
        if (null != role.getMenuIds()){
            for (Long menuId : role.getMenuIds()) {
                roleMenuMapper.insert(new RoleMenu(role.getId(), menuId));
            }
        }

    }

    @Override
    public ResponseResult deleteRoleMenu(Long id) {
        this.baseMapper.deleteById(id);
        //删除原有的权限
        LambdaQueryWrapper<RoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoleMenu::getRoleId,id);
        roleMenuMapper.delete(wrapper);

        return ResponseResult.okResult();
    }
    //获取id信息
    @Override
    public ResponseResult getRoleById(String id) {
        Role role = this.baseMapper.selectById(id);
        return ResponseResult.okResult(role);
    }

    @Override
    public ResponseResult getListAllRole() {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();

        List<Role> roleList = this.baseMapper.selectList(wrapper);

        return ResponseResult.okResult(roleList);
    }

    //更新角色
    @Override
    public ResponseResult updateRoleMenu(Role role) {
        //更新角色表
        this.baseMapper.updateById(role);
        //删除原有的权限
        LambdaQueryWrapper<RoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoleMenu::getRoleId,role.getId());
        roleMenuMapper.delete(wrapper);
        //新增加权限
        if (null != role.getMenuIds()){
            for (Long menuId : role.getMenuIds()) {
                roleMenuMapper.insert(new RoleMenu(role.getId(),menuId));
            }
        }
        return ResponseResult.okResult();
    }
}
